home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / russell / gc.lha / obj_map.c < prev    next >
C/C++ Source or Header  |  1993-03-04  |  4KB  |  128 lines

  1. /* 
  2.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3.  * Copyright (c) 1991, 1992 by Xerox Corporation.  All rights reserved.
  4.  *
  5.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  7.  *
  8.  * Permission is hereby granted to copy this garbage collector for any purpose,
  9.  * provided the above notices are retained on all copies.
  10.  */
  11.   
  12. /* Routines for maintaining maps describing heap block
  13.  * layouts for various object sizes.  Allows fast pointer validity checks
  14.  * and fast location of object start locations on machines (such as SPARC)
  15.  * with slow division.
  16.  *
  17.  * Boehm, February 6, 1992 1:00:09 pm PST
  18.  */
  19.  
  20. # include "gc_private.h"
  21.  
  22. char * GC_invalid_map = 0;
  23.  
  24. /* Invalidate the object map associated with a block.    Free blocks    */
  25. /* are identified by invalid maps.                    */
  26. void GC_invalidate_map(hhdr)
  27. hdr *hhdr;
  28. {
  29.     register int displ;
  30.     
  31.     if (GC_invalid_map == 0) {
  32.         GC_invalid_map = GC_scratch_alloc(MAP_SIZE);
  33.         for (displ = 0; displ < HBLKSIZE; displ++) {
  34.             MAP_ENTRY(GC_invalid_map, displ) = OBJ_INVALID;
  35.         }
  36.     }
  37.     hhdr -> hb_map = GC_invalid_map;
  38. }
  39.  
  40. /* Consider pointers that are offset bytes displaced from the beginning */
  41. /* of an object to be valid.                                            */
  42. void GC_register_displacement(offset) 
  43. word offset;
  44. {
  45. # ifndef ALL_INTERIOR_POINTERS
  46.     DCL_LOCK_STATE;
  47.     
  48.     DISABLE_SIGNALS();
  49.     LOCK();
  50.     GC_register_displacement_inner(offset);
  51.     UNLOCK();
  52.     ENABLE_SIGNALS();
  53. # endif
  54. }
  55.  
  56. void GC_register_displacement_inner(offset) 
  57. word offset;
  58. {
  59. # ifndef ALL_INTERIOR_POINTERS
  60.     register int i;
  61.     
  62.     if (offset > MAX_OFFSET) {
  63.         ABORT("Bad argument to GC_register_displacement");
  64.     }
  65.     if (!GC_valid_offsets[offset]) {
  66.       GC_valid_offsets[offset] = TRUE;
  67.       GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
  68.       for (i = 0; i <= MAXOBJSZ; i++) {
  69.           if (GC_obj_map[i] != 0) {
  70.              if (i == 0) {
  71.                GC_obj_map[i][offset + HDR_BYTES] = offset >> 2;
  72.              } else {
  73.                register int j;
  74.                register int lb = WORDS_TO_BYTES(i);
  75.                
  76.                if (offset < lb) {
  77.                  for (j = offset + HDR_BYTES; j < HBLKSIZE; j += lb) {
  78.                    GC_obj_map[i][j] = offset >> 2;
  79.                  }
  80.                }
  81.              }
  82.           }
  83.       }
  84.     }
  85. # endif
  86. }
  87.  
  88.  
  89. /* Add a heap block map for objects of size sz to obj_map.  */
  90. void GC_add_map_entry(sz)
  91. word sz;
  92. {
  93.     register int obj_start;
  94.     register int displ;
  95.     register char * new_map;
  96.     
  97.     if (sz > MAXOBJSZ) sz = 0;
  98.     if (GC_obj_map[sz] != 0) {
  99.         return;
  100.     }
  101.     new_map = GC_scratch_alloc(MAP_SIZE);
  102. #   ifdef PRINTSTATS
  103.         GC_printf1("Adding block map for size %lu\n", (unsigned long)sz);
  104. #   endif
  105.     for (displ = 0; displ < HBLKSIZE; displ++) {
  106.         MAP_ENTRY(new_map,displ) = OBJ_INVALID;
  107.     }
  108.     if (sz == 0) {
  109.         for(displ = 0; displ <= MAX_OFFSET; displ++) {
  110.             if (OFFSET_VALID(displ)) {
  111.                 MAP_ENTRY(new_map,displ+HDR_BYTES) = BYTES_TO_WORDS(displ);
  112.             }
  113.         }
  114.     } else {
  115.         for (obj_start = HDR_BYTES;
  116.              obj_start + WORDS_TO_BYTES(sz) <= HBLKSIZE;
  117.              obj_start += WORDS_TO_BYTES(sz)) {
  118.              for (displ = 0; displ < WORDS_TO_BYTES(sz); displ++) {
  119.                  if (OFFSET_VALID(displ)) {
  120.                      MAP_ENTRY(new_map, obj_start + displ) =
  121.                                      BYTES_TO_WORDS(displ);
  122.                  }
  123.              }
  124.         }
  125.     }
  126.     GC_obj_map[sz] = new_map;
  127. }
  128.